package org.zerg.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zerg.dao.ImageDao;
import org.zerg.dao.ThemeDao;
import org.zerg.dto.AnotherFileDto;
import org.zerg.dto.FileDto;
import org.zerg.entity.Image;
import org.zerg.entity.Product;
import org.zerg.entity.Theme;
import org.zerg.service.ThemeService;

@Service
public class ThemeServiceImpl extends BaseServiceImpl implements ThemeService{
	
	@Autowired
	private ThemeDao themeDao;
	
	@Autowired
	private ImageDao imageDao;
	
	@Value("${adImage.url}")
	private String adImageUrl;

	public List<Theme> getThemeList() {
		List<Theme> list = themeDao.getThemeList(0, 3);
		for (Theme theme : list) {
			theme.getTopicImg().setUrl(adImageUrl+theme.getTopicImg().getUrl());
			theme.getHeadImg().setUrl(adImageUrl+theme.getHeadImg().getUrl());
		}
		return list;
	}
	
	public Theme getThemeById(int id){
		Theme theme = themeDao.getThemeById(id);
		List<Product> list = themeDao.getProductByTheme(id);
		theme.getHeadImg().setUrl(adImageUrl+theme.getHeadImg().getUrl());
		for (Product product : list) {
			product.setMainImgUrl(adImageUrl+product.getMainImgUrl());
		}
		theme.setProduct(list);
		return theme;
	}

	public void modifyTheme(FileDto dto,AnotherFileDto andto,int id, String description) {
		String fileName = this.saveFile(dto);
		String fileName2 = this.saveAnotherFile(andto);
		imageDao.addImage("/"+fileName);
		imageDao.addImage("/"+fileName2);
		Image image = imageDao.getImage("/"+fileName);
		Image image2 = imageDao.getImage("/"+fileName2);
		themeDao.modifyTheme(id, image.getId(), image2.getId(), description);
	}

	public void modifyThemeNoTop(AnotherFileDto andto, int id,
			String description) {
		String fileName2 = this.saveAnotherFile(andto);
		imageDao.addImage("/"+fileName2);
		Image image2 = imageDao.getImage("/"+fileName2);
		themeDao.modifyThemeNoTop(id,image2.getId(),description);
	}

	public void modifyThemeNoHead(FileDto dto, int id, String description) {
		String fileName = this.saveFile(dto);
		imageDao.addImage("/"+fileName);
		Image image = imageDao.getImage("/"+fileName);
		themeDao.modifyThemeNoHead(id,image.getId(),description);
	}

	public void modifyThemeNoImage(int id, String description) {
		themeDao.modifyThemeNoImage(id,description);
	}

	public List<Product> getProductByTheme(int id) {
		List<Product> list = themeDao.getProductByTheme(id);
		for (Product product : list) {
			product.setMainImgUrl(adImageUrl+product.getMainImgUrl());
		}
		return list;
	}

	public void inserThemeOnProduct(int themeid, int pid) {
			themeDao.addThemeOnProduct(themeid, pid);
	}

	public Theme deleteProductOnTheme(int id, String desc) {
		Theme theme = themeDao.getThemeByName(desc);
		themeDao.themeDeleteProduct(theme.getId(), id);
		return theme;
	}

}
